<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="78px"
    >

      <el-form-item label="砍价名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入砍价名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="砍价状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择砍价状态"
          clearable
          size="small"
        >
          <el-option label="关闭" :value="0" />
          <el-option label="开启" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openForm(undefined)"
          v-hasPermi="['store:bargain:create']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['store:bargain:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="砍价图片" align="center">
        <template #default="{ row }">
          <el-image
            v-if="row.image"
            style="width: 70px; height: 70px; cursor: pointer; float: left"
            :src="row.image"
            :preview-src-list="[row.image]"
            hide-on-click-modal
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="砍价名称" align="center" prop="title" width="200">
        <template #default="{ row }">
          <el-tooltip v-if="row.title && row.title.length > 12" effect="light" placement="right" :disabled="false" :enterable="false">
            <template slot="content">
              <div style="max-width: 300px; word-break: break-all; line-height: 1.5; padding: 8px;">
                {{ row.title }}
              </div>
            </template>
            <span class="bargain-title" :title="row.title">{{ row.title }}</span>
          </el-tooltip>
          <span v-else class="bargain-title" :title="row.title">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="砍价价格" align="center" prop="price" />
      <el-table-column label="最低价" align="center" prop="minPrice" />
      <el-table-column label="参与砍价人数" align="center" prop="participateCount" width="140"/>
      <el-table-column label="帮忙砍价人数" align="center" prop="peopleNum" width="140"/>
      <el-table-column label="成功砍价人数" align="center" prop="successCount" width="140"/>
      <el-table-column label="限量" align="center" prop="quota" />
      <el-table-column label="限量剩余" align="center" prop="quotaShow" />
      <el-table-column label="活动时间" align="center" prop="startTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="砍价结束时间" align="center" prop="stopTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.stopTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="砍价状态"
        align="center"
        prop="status"
      >
        <template v-slot="scope">
          <span :style="{ color: scope.row.status === 1 ? '#67C23A' : '#F56C6C' }">
            {{ scope.row.status === 1 ? '开启' : '关闭' }}
          </span>
        </template>
      </el-table-column>


      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="140"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openForm(scope.row.id)"
            v-hasPermi="['store:bargain:update']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:bargain:delete']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 对话框(添加 / 修改) -->
    <BargainForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
import * as BargainApi from "@/api/store/bargain";
import BargainForm from "./BargainForm.vue";
import { getProductSpecList } from '@/api/store/product';

export default {
  name: "Bargain",
  components: {
    BargainForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 砍价列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        productId: null,
        title: null,
        image: null,
        status: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await BargainApi.getBargainPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total || 0;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除砍价编号为"' + id + '"的数据项?');
      try {
        await BargainApi.deleteBargain(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm("是否确认导出所有砍价数据项?");
      try {
        this.exportLoading = true;
        const data = await BargainApi.exportBargainExcel(this.queryParams);
        this.$download.excel(data, "砍价.xls");
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
    async fetchProductSpecList() {
      if (!this.formData.productId) {
        this.$message.warning('请先填写商品ID');
        return;
      }
      try {
        const res = await getProductSpecList(this.formData.productId);
        this.specList = res.data || [];
        this.$message.success('商品属性已加载');
      } catch (e) {
        this.$message.error('查询商品属性失败');
      }
    },
  },
};
</script>

<style scoped>
.bargain-title {
  display: inline-block;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
  cursor: pointer;
}
</style>
